package visualizer.client.widgets.messageBox;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.*;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.EventListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;

/**
 * User: DDovzhenko
 * Date: 26.01.14
 * Time: 12:56
 */
public class MessageBoxView extends Composite implements MessageBoxPresenter.Display {
    @UiField
    ParagraphElement message;
    @UiField
    AnchorElement closeButton;

    public MessageBoxView() {
        initWidget(ourUiBinder.createAndBindUi(this));
        DOM.sinkEvents(closeButton.<Element>cast(), Event.ONCLICK);
        DOM.setEventListener(closeButton.<Element>cast(), new EventListener() {
            @Override
            public void onBrowserEvent(Event event) {
                setVisibility(false);
            }
        });
    }

    @Override
    public void setMessage(String message) {
        this.message.setInnerHTML(message);
    }

    @Override
    public void setVisibility(boolean isVisible) {
        if (isVisible) {
            getElement().getStyle().setVisibility(Style.Visibility.VISIBLE);
        } else {
            getElement().getStyle().setVisibility(Style.Visibility.HIDDEN);
        }
    }

    //------------------UI Binder--------------------
    interface MessageBoxUiBinder extends UiBinder<HTMLPanel, MessageBoxView> {  }
    private static MessageBoxUiBinder ourUiBinder = GWT.create(MessageBoxUiBinder.class);
}